﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace PBLOG
{
    internal static class LogsCollectionHelper
    {
        private static List<Log> _logs;

        public static List<Log> Logs
        {
            get { return _logs; }
            set { _logs = value; }
        }

        public static void Add(Log item)
        {
            if (item.User != null)
            {
                if (!XmlHelper.Users.ContainsUser(item.User))
                    throw new ArgumentNullException("If you add a log with a user, the user must be exists.");
            }
            else
            {
                DateTime oldDt = item.DateTime;
                DateTime dtNow = DateTime.Now;

                if (item.DateTime.IsNull() || item.DateTime != DateTime.Now) item.DateTime = dtNow;

                _logs.Add(item);

                XmlHelper.Logs.WriteLog(item);

                if (!oldDt.IsNull())
                {
                    item.DateTime = oldDt;
                }
                else
                {
                    item.DateTime = dtNow;
                }
            }
        }

        public static void Add(string message, LogType logType)
        {
            Log log = new Log()
            {
                Message = message,
                LogType = logType,
                DateTime = DateTime.Now
            };

            _logs.Add(log);

            XmlHelper.Logs.WriteLog(log);
        }

        public static void Add(string message, string username, LogType logType)
        {
            if (String.IsNullOrWhiteSpace(username))
                throw new ArgumentNullException("'Username' is required.");

            Log log = new Log()
            {
                DateTime = DateTime.Now,
                Message = message,
                LogType = logType,
                User = XmlHelper.Users.GetUsers().Where(u => u.Username == username).Single()
            };

            XmlHelper.Logs.WriteLog(log);
        }

        public static void Add(string message, User user, LogType logType)
        {
            if (user == null)
                throw new ArgumentNullException("'User' is required.");

            Log log = new Log()
            {
                DateTime = DateTime.Now,
                Message = message,
                LogType = logType,
                User = user
            };

            XmlHelper.Logs.WriteLog(log);
        }

        public static void Clear()
        {
            _logs.Clear();

            XmlHelper.Logs.DeleteAllLogs();
        }

        public static void Clear(LogType logType)
        {
            XmlHelper.Logs.DeleteByType(logType);
        }

        public static bool Contains(Log item)
        {
            return XmlHelper.Logs.ContainsLog(item);
        }

        public static bool Contains(string message)
        {
            Log log = new Log();
            log.Message = message;

            return XmlHelper.Logs.ContainsLog(log);
        }

        public static void CopyTo(Log[] array, int arrayIndex)
        {
            _logs.CopyTo(array, arrayIndex);
        }

        public static int Count
        {
            get { return _logs.Count; }
        }

        public static bool IsReadOnly
        {
            get { return false; }
        }

        public static bool Remove(Log item)
        {
            XmlHelper.Logs.DeleteLog(item);

            return _logs.Remove(item);
        }

        public static IEnumerator<Log> GetEnumerator()
        {
            return _logs.GetEnumerator();
        }
    }
}
